using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using DBA.DataBaseAccess;

namespace BL.ServerBL
{
    partial class ServerRemotingCommon : BL.BAClientRemotingInterface.IBudgetApprove
    {
        public bool HaveBAAuthority(string budget_id, string user_id)
        {
            if (!DBAccess.UserApproveAuthorityEqual(budget_id, user_id))
                return false;
            if (!DBAccess.IsBudgetBelongUsersDept(budget_id, user_id))
                return false;
            return true;
        }

        public bool HaveFBAAuthority(string budget_id, string user_id)
        {
            if (!DBAccess.UserApproveAuthorityEqual(budget_id, user_id))
                return false;
            else
                return true;
        }

        public bool BudgetApprove(string budget_id, string user_id, bool isapproved)
        {
            string budget_state_id_before = DBAccess.GetBudgetStateIdByBudgetId(budget_id);           
            string budget_state_id_after = DBAccess.GetNextApprovedStateId(budget_id, isapproved);
            DBAccess.ChangeBudgetStateId(budget_id, budget_state_id_after);
            DBAccess.InsertApproveLog(budget_id, budget_state_id_before, budget_state_id_after,
                user_id, null, isapproved, false);
            if (DBAccess.IsBudgetAllApproved(budget_id))
            {
                DBAccess.ChangeIsAllApproved(budget_id, true);
            }
            return true;
        }

        public bool FBudgetApprove(string budget_id, string user_id,
            bool isapproved, string budget_approve_needcount)
        {
            string budget_state_id_before = DBAccess.GetBudgetStateIdByBudgetId(budget_id);
            string budget_state_id_after = DBAccess.GetNextApprovedStateId(budget_id, isapproved);
            DBAccess.ChangeBudgetStateId(budget_id, budget_state_id_after);
            DBAccess.ChangeBudgetApproveNeedCount(budget_id, budget_approve_needcount);
            DBAccess.InsertApproveLog(budget_id, budget_state_id_before, budget_state_id_after,
                user_id, null, isapproved, true);
            return true;
        }

        public DataSet GetBudgetToApprove(string user_id)
        {
            string budget_state_id = DBAccess.GetBudgetStateToApprove(user_id);
            string department_id = DBAccess.GetDepartmentIdByUserId(user_id);
            return DBAccess.GetBudgetInfo(null, null, department_id, null, null, budget_state_id, false, true);
        }

        public DataSet GetBudgetToFApprove(string user_id)
        {
            string budget_state_id = DBAccess.GetBudgetStateToApprove(user_id);
            return DBAccess.GetBudgetInfo(null, null, null, null, null, budget_state_id, true);
        }
        public DataSet GetBudgetApproveNeedCountInfo()
        {
            return DBAccess.GetBudgetApproveNeedCountInfo();
        }

    }
}
